//
//  KLVistorView.swift
//  新浪微博
//
//  Created by WTFKL on 17/8/10.
//  Copyright © 2017年 WTF. All rights reserved.
//

import UIKit

class KLVistorView: UIView {
    
    
    var visitDict : [String : String]?{
        didSet{
          guard let imageName = visitDict?["imageName"],
            let message  = visitDict?["message"] else{
                return;
            }
            
            tipLabel.text = message;
            
            if imageName == "" {
                setAnimation();
                return;
            }
            
            houseView.image = UIImage(named: imageName);
            cycleView.isHidden = true;
            bgView.isHidden = true;
        }
    }
    
  lazy var loginBtn : UIButton = UIButton(title: "登录", normalColor: #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1), highLightColor: #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1), bgColor: #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1),normalImage:#imageLiteral(resourceName: "common_button_white_disable"));
  lazy var registBtn :UIButton = UIButton(title: "注册", normalColor: #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1), highLightColor: #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1), bgColor: #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1),normalImage:#imageLiteral(resourceName: "common_button_white_disable"));

  fileprivate lazy var cycleView  =  UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"));
  fileprivate lazy var houseView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
  fileprivate lazy var tipLabel = UILabel(text: "关注一些人，回这里看看有什么惊喜", fontSize: 14, color: UIColor.black);
  fileprivate lazy var bgView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"));
    
    override init(frame: CGRect) {
        super.init(frame:frame);
        setupUI();
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
}


extension KLVistorView{
    //set ui
   fileprivate func setupUI(){
        self.backgroundColor = UIColor.init(colorLiteralRed: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1);
        tipLabel.numberOfLines = 0;
        tipLabel.textAlignment = NSTextAlignment.center;
        //add child view
        addSubview(cycleView);
        addSubview(bgView);
        addSubview(houseView);
        addSubview(loginBtn);
        addSubview(registBtn);
        addSubview(tipLabel);
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false;
        }
        
        //圆圈
        addConstraint(NSLayoutConstraint(item: cycleView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0));
        
        addConstraint(NSLayoutConstraint(item: cycleView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: -60));
        //house
        
        addConstraint(NSLayoutConstraint(item: houseView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: cycleView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0));
        
        addConstraint(NSLayoutConstraint(item: houseView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: cycleView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0));
        
        
        //label
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: cycleView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20));
        
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: cycleView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0));
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 300));
        //login_button
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20));
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .leading,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .leading,
                                         multiplier: 1.0,
                                         constant: 0));
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100));
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .height,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 30));
        
        
        //regist_button
        addConstraint(NSLayoutConstraint(item: registBtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20));
        addConstraint(NSLayoutConstraint(item: registBtn,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0));
        addConstraint(NSLayoutConstraint(item: registBtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100));
        addConstraint(NSLayoutConstraint(item: registBtn,
                                         attribute: .height,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 30));
       
        
        let viewDict : [String : Any?] = ["bgView":bgView,"registBtn" : registBtn];
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-0-[bgView]-0-|",
            options: [],
            metrics: nil,
            views: viewDict));
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-0-[bgView]-(-35)-[registBtn]",
            options: [],
            metrics: nil,
            views: viewDict));
        
    }
    
  fileprivate func setAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation");
        anim.toValue = 2 * M_PI;
        anim.repeatCount = MAXFLOAT;
        anim.duration = 15;
        anim.isRemovedOnCompletion = false;
        cycleView.layer.add(anim, forKey: nil);
    }
    
    
    
    
}
